<?php

function list_disciplinary($periode_id){
	if( $periode_id==0 ){
		$periode_id=get_periode_absen();
	}	
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$periode=substr($periodes['tanggal'],0,7) ;
	my_set_code_js('	
		function postDataPeriode(periode_id){ 
			location.href=\'index.php?com='.$_GET['com'].'&periode_id=\'+periode_id ;
		}
	');
	
	$header = array(
		'Tanggal'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Pelanggaran'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Jenis Peringatan'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Tindakan yang Diberikan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
	);
	
	
	$query =  "SELECT * FROM tatib_detail a 
			INNER JOIN tatib b ON a.tatib_id = b.tatib_id 
			INNER JOIN jenis_peringatan c ON c.jenis_peringatan_id=a.jenis_peringatan_id";
	if( $periode<>'Semua'){		
		$query .=  " WHERE LEFT(b.tanggal,7)='{$periode}'";  	
	}		
	$query .=  " ORDER BY b.tanggal DESC";
			//var_dump( $query ); exit;
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =15;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array();
	while($ey = my_fetch_array($result) ){
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		$row[] = array(
			'#'=>date('d-m-Y', strtotime($ey['tanggal'])), 
			'nik'=>  $karyawan['nik'] ,
			'nama'=> $karyawan['nama_gelar'] ,   
			'uraian'=> $ey['uraian']  ,   
			'ke'=> $ey['nama_jenis_peringatan']  ,   
			'sangsi'=> $ey['sangsi']  ,   
		);
		
	}	
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();
	$menuontop = array(
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=rpt_disciplinary&periode_id='.$periode_id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel&periode_id='.$periode_id.'\';')
	);
	$query = " SELECT * FROM pr_periode ORDER BY periode_id DESC";
	$result = my_query($query);
	$prs = "";
	while($prow = my_fetch_array($result)){
		if($prow['periode_id'] ==$periode_id){
			$prs .='<option selected value="'.$prow['periode_id'].'">'.date('m-Y',strtotime($prow['tanggal'])).'</option>';
		}else{
			$prs .='<option value="'.$prow['periode_id'].'">'.date('m-Y',strtotime($prow['tanggal'])).'</option>';
		}	
	}
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'"/>
	<table width="100%">
	<tr>
		<td>Periode</td>	 
		<td><select name="periode_id" id="periode_id" onchange="javascript:postDataPeriode(this.value)">'.$prs.'</select></td>				
	</tr>
	</table>
	</form>';
	$printmenu = additional_menu_on_list( $menuontop , $search);	
	return $printmenu.table_builder($header , $datas ,  15 , false  , $paging  );
	
}

function excel_report($periode_id){
	my_component_load('xl_builder' , false);

	if( $periode_id==0 ){
		$periode_id=get_periode_absen();
	}	
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$periode=substr($periodes['tanggal'],0,7) ;
	
	$header = array(
		'Tanggal'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Pelanggaran'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Jenis Peringatan'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Tindakan yang Diberikan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
	);
	
	
	$query =  "SELECT * FROM tatib_detail a 
			INNER JOIN tatib b ON a.tatib_id = b.tatib_id 
			INNER JOIN jenis_peringatan c ON c.jenis_peringatan_id=a.jenis_peringatan_id";
	if( $periode<>'Semua'){		
		$query .=  " WHERE LEFT(b.tanggal,7)='{$periode}'";  	
	}		
	$query .=  " ORDER BY b.tanggal DESC";
	$result = my_query($query );

	$row = array(); 
	$i = 0; 
	while($ey = my_fetch_array($result )){
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		$row[] = array(
			'#'=>date('d-m-Y', strtotime($ey['tanggal'])), 
			'nik'=>  $karyawan['nik'] ,
			'nama'=> $karyawan['nama_gelar'] ,   
			'uraian'=> $ey['uraian']  ,   
			'ke'=> $ey['nama_jenis_peringatan']  ,   
			'sangsi'=> $ey['sangsi']  ,   
		);
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 15 ,false ); 
}